import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 创建学生管理系统实例
        StudentManagementSystem studentSystem = new StudentManagementSystem();
        
        // 创建学生关系系统实例
        StudentRelationshipSystem relationshipSystem = new StudentRelationshipSystem();
        
        // 尝试从文件加载学生数据
        try {
            studentSystem.loadStudentsFromFile();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载学生数据失败，使用空的学生列表开始");
        }
        
        // 创建扫描器用于获取用户输入
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            // 显示菜单
            System.out.println("\n学生关联系统");
            System.out.println("1. 添加学生");
            System.out.println("2. 查看所有学生");
            System.out.println("3. 查找学生");
            System.out.println("4. 添加学生关系");
            System.out.println("5. 查看学生关系");
            System.out.println("6. 保存学生数据到文件");
            System.out.println("7. 退出");
            System.out.print("请选择操作(1-7): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区
            
            try {
                switch (choice) {
                    case 1:
                        // 添加学生
                        System.out.print("请输入学生ID: ");
                        int id = scanner.nextInt();
                        scanner.nextLine(); // 清除缓冲区
                        
                        System.out.print("请输入学生姓名: ");
                        String name = scanner.nextLine();
                        
                        System.out.print("请输入学生性别: ");
                        String gender = scanner.nextLine();
                        
                        System.out.print("请输入学生年龄: ");
                        int age = scanner.nextInt();
                        
                        System.out.print("请输入学生学号: ");
                        scanner.nextLine(); // 清除缓冲区
                        String studentNumber = scanner.nextLine();
                        
                        Student student = new Student(id, name, gender, age, studentNumber);
                        studentSystem.addStudent(student);
                        // 同时将学生添加到关系系统中
                        relationshipSystem.addStudent(id);
                        System.out.println("学生添加成功!");
                        break;
                    
                    case 2:
                        // 查看所有学生
                        List<Student> allStudents = studentSystem.getAllStudents();
                        System.out.println("\n所有学生信息:");
                        for (Student student : allStudents) {
                            System.out.println(student);
                        }
                        break;
                    
                    case 3:
                        // 查找学生
                        System.out.print("请输入要查找的学生ID: ");
                        int searchId = scanner.nextInt();
                        Student foundStudent = studentSystem.getStudentById(searchId);
                        if (foundStudent != null) {
                            System.out.println("找到学生: " + foundStudent);
                        } else {
                            System.out.println("未找到ID为" + searchId + "的学生");
                        }
                        break;
                    
                    case 4:
                        // 添加学生关系
                        System.out.print("请输入第一个学生的ID: ");
                        int id1 = scanner.nextInt();
                        
                        System.out.print("请输入第二个学生的ID: ");
                        int id2 = scanner.nextInt();
                        
                        System.out.println("选择关系类型:");
                        System.out.println("1. 同学");
                        System.out.println("2. 室友");
                        System.out.println("3. 朋友");
                        System.out.println("4. 团队成员");
                        System.out.print("请输入选项(1-4): ");
                        int typeChoice = scanner.nextInt();
                        
                        RelationshipType type;
                        switch (typeChoice) {
                            case 1:
                                type = RelationshipType.CLASSMATE;
                                break;
                            case 2:
                                type = RelationshipType.ROOMMATE;
                                break;
                            case 3:
                                type = RelationshipType.FRIEND;
                                break;
                            case 4:
                                type = RelationshipType.TEAM_MEMBER;
                                break;
                            default:
                                System.out.println("无效的选择，默认设置为同学关系");
                                type = RelationshipType.CLASSMATE;
                        }
                        
                        try {
                            relationshipSystem.addRelationship(id1, id2, type);
                            System.out.println("关系添加成功!");
                        } catch (IllegalArgumentException e) {
                            System.out.println("添加关系失败: " + e.getMessage());
                        }
                        break;
                    
                    case 5:
                        // 查看学生关系
                        System.out.print("请输入要查看关系的学生ID: ");
                        int relationId = scanner.nextInt();
                        
                        Map<Integer, RelationshipType> relations = relationshipSystem.getRelationships(relationId);
                        if (relations.isEmpty()) {
                            System.out.println("该学生没有建立任何关系");
                        } else {
                            System.out.println("学生的关系:");
                            for (Map.Entry<Integer, RelationshipType> entry : relations.entrySet()) {
                                System.out.println("与ID为" + entry.getKey() + "的学生是" + entry.getValue());
                            }
                        }
                        break;
                    
                    case 6:
                        // 保存学生数据到文件
                        try {
                            studentSystem.saveStudentsToFile();
                        } catch (IOException e) {
                            System.out.println("保存学生数据失败: " + e.getMessage());
                        }
                        break;
                    
                    case 7:
                        // 退出系统
                        System.out.println("感谢使用学生关联系统，再见!");
                        scanner.close();
                        return;
                    
                    default:
                        System.out.println("无效的选择，请输入1-7之间的数字");
                }
            } catch (Exception e) {
                System.out.println("发生错误: " + e.getMessage());
                scanner.nextLine(); // 清除错误输入
            }
        }
    }
}